import { Topic } from "../../models/Topic";

export default defineEventHandler(async (event) => {
  const method = getMethod(event);
  console.log(method,'method');
  
  // 获取所有题目
  if (method === "GET") {
    try {
      const topics = await Topic.find({});
      return topics;
    } catch (error) {
      throw createError({
        statusCode: 500,
        message: "获取题目列表失败",
      });
    }
  }

  // 创建新题目
  if (method === "POST") {
    try {
      const body = await readBody(event);
      const topic = await Topic.create(body);
      return topic;
    } catch (error) {
      throw createError({
        statusCode: 400,
        message: "创建题目失败",
      });
    }
  }
});
